<!--#
layout("/layouts/platform.html"){
#-->
<div id="app" v-cloak>
    <el-row class="header navbar bg-white shadow">
        <div class="btn-group tool-button mt5">
            <el-button size="medium" @click="openAdd()">
                <i class="ti-plus"></i>
                新增

            </el-button>
        </div>

    </el-row>
    <div class="btn-group tool-button mt5">
        <el-input placeholder="请输入内容" v-model="pageForm.searchKeyword" @keyup.enter.native="pageData">
            <el-button slot="append" icon="el-icon-search" @click="pageData"></el-button>
        </el-input>
    </div>
    <el-row class="el-table-container" style="padding-top: 15px">
        <el-table
                :data="tableData"
                @sort-change='pageOrder'
                size="small"
                header-align="center"
                style="width: 100%"
                @selection-change="handleSelectionChange"
                :default-sort="{prop: 'opAt', order: 'descending'}"
        >
            <el-table-column
                    type="selection"
                    width="35">
            </el-table-column>

            <el-table-column label="名称" header-align="left" prop="name"
                             :show-overflow-tooltip="true">
            </el-table-column>

            <el-table-column label="价格" header-align="left" prop="price"
                             :show-overflow-tooltip="true">
            </el-table-column>

            <el-table-column sortable label="创建时间" header-align="center" align="center" prop="opAt">
                <template slot-scope="scope">
                    {{formatAt(scope.row.opAt)}}
                </template>
            </el-table-column>

            <el-table-column label="图片" header-align="left" prop="channel"
                             :show-overflow-tooltip="true">
                <template scope="scope">

                    <p v-if="scope.row.coverPic">
                        <img :src="scope.row.coverPic" style="width: 60px;height: 60px">
                    </p>
                    <p v-else>
                        暂无数据
                    </p>
                </template>
            </el-table-column>
            <el-table-column label="状态" header-align="left" prop="channel"
                             :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <el-switch active-color="#ff4949" inactive-color="#13ce66" v-model="scope.row.disabled"
                               @change="change(scope.row)">
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column
                    prop="userOnline"
                    label="操作"
                    width="120">
                <template slot-scope="scope">
                    <el-dropdown @command="dropdownCommand">
                        <el-button size="mini">
                            <i class="ti-settings"></i>
                            <span class="ti-angle-down"></span>
                        </el-button>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item divided :command="{type:'edit',id:scope.row.id}">
                                修改
                            </el-dropdown-item>
                            <el-dropdown-item :command="{type:'delete',id:scope.row.id}">
                                删除
                            </el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                </template>
            </el-table-column>
        </el-table>
    </el-row>
    <el-row class="el-pagination-container">
        <el-pagination
                @size-change="pageSizeChange"
                @current-change="pageNumberChange"
                :current-page="pageForm.pageNumber"
                :page-sizes="[10, 20, 30, 50]"
                :page-size="pageForm.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pageForm.totalCount">
        </el-pagination>
    </el-row>

    <el-dialog
            :title="formData.id==null?'新增':'修改'"
            :visible.sync="addDialogVisible"
            width="40%">
        <el-form :model="formData" ref="addForm" :rules="formRules" size="small" label-width="80px">
            <el-form-item prop="name" label="名称">
                <el-input maxlength="100" placeholder="名称"
                          v-model="formData.name"
                          auto-complete="off" tabindex="1" type="text"></el-input>
            </el-form-item>
            <el-form-item prop="price" label="价格">
                <el-input maxlength="100" placeholder="价格"
                          v-model="formData.price"
                          auto-complete="off" tabindex="2" type="number"></el-input>
            </el-form-item>

            <el-form-item prop="coverPic" label="封面图">
                <el-upload
                        class="avatar-uploader-fm"
                        action="${base}/open/file/upload/image2"
                        name="Filedata"
                        :show-file-list="false"
                        :on-success="function(resp,file,fileList){return handlePicSuccess(resp,file,fileList,'coverPic')}">

                    <img v-if="formData.coverPic" :src="'${AppFileDomain!}'+formData.coverPic" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
                    <el-button @click="addDialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="doAdd">确 定</el-button>
                  </span>
    </el-dialog>

</div>
<style>
    .avatar-uploader-fm .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader-fm.el-upload:hover {
        border-color: #20a0ff;
    }
    .avatar-uploader-fm .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 150px;
        height: 150px;
        line-height: 150px;
        text-align: center;
    }
    .avatar-uploader-fm .avatar {
        width: 150px;
        height: 150px;
        display: block;
    }
    .avatar-uploader-fm .el-upload__input{
        display: none;
    }
</style>
<script>
    new Vue({
        el: "#app",
        data: function () {
            return {
                addDialogVisible: false,
                editDialogVisible: false,
                selectData: [],
                pageForm: {
                    searchName: "",
                    searchKeyword: "",
                    pageNumber: 1,
                    pageSize: 10,
                    totalCount: 0,
                    pageOrderName: "opAt",
                    pageOrderBy: "descending"
                },
                tableData: [],
                formData: {
                    id: null,
                },
                formRules: {
                    name: [
                        {required: true, message: '名称', trigger: 'blur'}
                    ],
                    price: [
                        {required: true, message: '价格', trigger: 'blur'}
                    ],
                    coverPic: [
                        {required: true, message: '封面图', trigger: 'blur'}
                    ]
                },
                options: [],
                loading: false,
                cache:{}
            }
        },
        methods: {
            pageOrder: function (column) {//按字段排序
                this.pageForm.pageOrderName = column.prop;
                this.pageForm.pageOrderBy = column.order;
                this.pageData();
            },
            pageNumberChange: function (val) {//页码更新操作
                this.pageForm.pageNumber = val;
                this.pageData();
            },
            pageSizeChange: function (val) {//分页大小更新操作
                this.pageForm.pageSize = val;
                this.pageData();
            },
            pageData: function () {//加载分页数据
                var self = this;
                sublime.showLoadingbar();//显示loading
                $.post(base + "/platform/gift/manager/data", self.pageForm, function (data) {
                    sublime.closeLoadingbar();//关闭loading
                    if (data.code == 0) {
                        self.tableData = data.data.list;
                        self.pageForm.totalCount = data.data.totalCount;
                    } else {
                        self.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                }, "json");
            },
            formatAt: function (val) {
                if (val > 0)
                    return moment(val * 1000).format("YYYY-MM-DD HH:mm");
                return "";
            },

            handleSelectionChange: function (val) {
                this.selectData = val;
            },
            openAdd: function (id)
            {
                var self = this;
                self.addDialogVisible = true;
                //打开新增窗口,表单先清空
                self.formData = {};
                self.formData.id = id;
                if (null != id) {
                    $.post(base + "/platform/gift/manager/edit", {id}, function (data) {
                        if (data.code == 0) {
                            self.formData = data.data;//加载后台表单数据
                        }
                    }, "json");


                }
                if (self.$refs["addForm"])
                    self.$refs["addForm"].resetFields();
            },
            doAdd: function () {
                if (null != this.formData.id) {
                    this.doEdit();
                    return;
                }
                var self = this;
                var url = base + "/platform/gift/manager/add";
                self.$refs["addForm"].validate(function (valid) {
                    if (valid) {//表单验证通过
                        $.post(url, self.formData, function (data) {
                            if (data.code == 0) {
                                self.addDialogVisible = false;
                                self.pageData()
                                self.$message({
                                    message: data.msg,
                                    type: 'success'
                                });
                                self.addDialogVisible = false;
                                self.pageData();
                            } else {
                                self.$message({
                                    message: data.msg,
                                    type: 'error'
                                });
                            }
                        }, "json");
                    }
                });
            },
            doEdit: function () {
                var self = this;
                var url = base + "/platform/gift/manager/editDo";
                self.$refs["addForm"].validate(function (valid) {
                    if (valid) {//表单验证通过
                        $.post(url, self.formData, function (data) {
                            if (data.code == 0) {
                                self.addDialogVisible = false;
                                self.pageData()
                                self.$message({
                                    message: data.msg,
                                    type: 'success'
                                });
                                self.editDialogVisible = false;
                                self.pageData();
                            } else {
                                self.$message({
                                    message: data.msg,
                                    type: 'error'
                                });
                            }
                        }, "json");
                    }
                });
            },
            dropdownCommand: function (command) {//监听下拉框事件
                if ("edit" == command.type) {
                    this.openAdd(command.id);
                } else
                    $.post(base + "/platform/gift/manager/" + command.type, command, function (data) {
                        if (data.code == 0) {
                            self.formData = data.data;//加载后台表单数据
                            self.editDialogVisible = true;//打开编辑窗口
                            this.pageData()
                        }
                    }, "json");
                this.pageData()
            },
            getAccounts: function (str) {
                var _this = this;
                $.post(base + "/platform/user/manager/searchAccount", {key: str}, function (data) {
                    _this.options = data.code == 0 ? data.data.list.map(ite => {
                            // ite.label =ite.nickName+'#<span style="color: darkblue">'+ite.phone+'</span>$<span style="color: darkred">'+ite.balance+'</span>'
                            ite.label = ite.nickName + '#' + ite.phone + '$' + ite.balance
                            return ite
                        }
                    ) : [];
                }, "json");
            },
            handlePicSuccess: function (response, file, fileList, attrName) {
                if (response.code == 0) {
                    // 对象属性直接赋值不会触发试图更新，采用 Vue.set 、this.$set或者Object.assign({}，this.obj)创建新对象
                    // 或者也可以定义一个 refresh 属性 ，当数据发生改变时，该属性同时变化，也会触发视图更新
                    // this.formData.site_logo =response.data;
                    this.$set(this.formData, attrName, response.data);
                } else {
                    this.$set(this.formData, attrName, "");
                }
            },
            change: function (row) {
                $.post(base + "/platform/gift/manager/disable", {disable: row.disabled, id: row.id}, function (data) {
                    this.pageData()
                })
            }
        },
        created: function () {
            this.pageData();
        }
    });
</script>
<!--#
}
#-->